package com.jiukuaidao.zhulin.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jiukuaidao.zhulin.R;

/**
 * T_winds
 * Date:  16.8.23
 * Description:
 */
public class MyRatingBar extends LinearLayout {

    public interface OnRatingListener{

        void onRating(Object bindObject,int RatingSource);

    }

    public boolean mClickable = true;
    private OnRatingListener onRatingListener;
    private Object bindObject;
    private float starImageSize;
    private int starCount;
    private Drawable starEmptyDrawable;
    private Drawable starFillDrawable;
    private int mStarCount;

    public MyRatingBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.HORIZONTAL);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyRatingBar);
        starImageSize = ta.getDimension(R.styleable.MyRatingBar_starImageSize,20);
        starCount = ta.getInteger(R.styleable.MyRatingBar_starCount,5);
        starEmptyDrawable = ta.getDrawable(R.styleable.MyRatingBar_starEmpty);
        starFillDrawable = ta.getDrawable(R.styleable.MyRatingBar_starFill);
        ta.recycle();

        for(int i=0;i<starCount;++i){
            ImageView imageView = getStarImageView(context,attrs);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mClickable){
                        mStarCount = indexOfChild(v) + 1;
                        setStar(mStarCount,true);
                        if (onRatingListener !=null){
                            onRatingListener.onRating(bindObject,mStarCount);
                        }
                    }
                }
            });
            addView(imageView);
        }
    }

    public void setClickable(boolean clickable){
        this.mClickable = clickable;
    }

    private ImageView getStarImageView(Context context,AttributeSet attrs){

        ImageView imageView = new ImageView(context);
        ViewGroup.LayoutParams para = new ViewGroup.LayoutParams(Math.round(starImageSize), Math.round(starImageSize));
        imageView.setLayoutParams(para);
        imageView.setPadding(0, 0, 20, 0);
        imageView.setImageDrawable(starEmptyDrawable);
        imageView.setMaxHeight(15);
        imageView.setMaxWidth(15);
        return  imageView;
    }

    public void setStar(int starCount,boolean animation){

        starCount = starCount > this.starCount ? this.starCount:starCount;
        starCount = starCount < 0 ? 0:starCount;

        for (int i=0;i<starCount;++i){

            ((ImageView) getChildAt(i)).setImageDrawable(starFillDrawable);
//            if (animation){//设置动画
//                ScaleAnimation scaleAnimation = new ScaleAnimation(0,0,1,1);
//                getChildAt(i).startAnimation(scaleAnimation);
//            }
        }

        for (int i=this.starCount - 1;i>=starCount;--i){

            ((ImageView) getChildAt(i)).setImageDrawable(starEmptyDrawable);
        }
    }

    public int getStarCount(){
        return this.mStarCount;
    }

    public void setStarFillDrawable(Drawable starFillDrawable){
        this.starFillDrawable = starFillDrawable;
    }

    public void setStarEmptyDrawable(Drawable starEmptyDrawable){
        this.starEmptyDrawable = starEmptyDrawable;
    }

    public void setStarCount(int starCount){
        this.starCount = starCount;
    }

    public void setStarImageSize(float starImageSize) {
        this.starImageSize = starImageSize;
    }

    public void setBindObject(Object bindObject) {
        this.bindObject = bindObject;
    }

    public void setOnRatingListener(OnRatingListener onRatingListener) {
        this.onRatingListener = onRatingListener;
    }
}
